package com.avic.rong.auditcertification.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.auditcertification.api.CertificationMsgApi;
import com.avic.rong.auditcertification.bean.CertificationMsgBean;
import com.avic.rong.auditcertification.fragment.ShenFenRzActivity;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import butterknife.BindView;


//个人认证主页
public class PersonAuthenticationHomeActivity extends BaseActivity {


    @BindView(R.id.loading_layout)
    LoadingLayout loadingLayout;
    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.img_personalResume)
    ImageView imgPersonalResume;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_field_ly)
    TextView tvFieldLy;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_introduce)
    TextView tvIntroduce;
    @BindView(R.id.img_home)
    ImageView imgHome;
    @BindView(R.id.tv_real_name)
    TextView tvRealName;
    @BindView(R.id.tv_job_title)
    TextView tvJobTitle;
    @BindView(R.id.tv_company_name)
    TextView tvCompanyName;
    @BindView(R.id.tv_honor)
    TextView tvHonor;
    @BindView(R.id.tv_publicCation)
    TextView tvPublicCation;
    @BindView(R.id.tv_projectexperience)
    TextView tvProjectexperience;
    @BindView(R.id.btn_authentionhome)
    Button btnAuthentionhome;
    @BindView(R.id.ll_nothrough)
    LinearLayout linearLayout;


    private CertificationMsgApi certificationMsgApi;

    private static int FLAG;
    private CertificationMsgBean cerBean;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_personalhome_authentication;
    }

    @Override
    protected void initView() {
        loadingLayout.setStatus(LoadingLayout.LOADING);
        tvTitle.setText("个人认证");
        ivRight.setBackgroundResource(R.mipmap.slh);
        loadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                HttpManager.getInstance().doHttpDeal(certificationMsgApi);
                loadingLayout.setStatus(LoadingLayout.LOADING);
            }

            @Override
            public void onReturn(View v) {
                onBackPressed();
            }
        });


        certificationMsgApi = new CertificationMsgApi(this, httpOnNextListener);
        certificationMsgApi.setToken(SharedPreferencesUtil.getLoginToken());
        HttpManager.getInstance().doHttpDeal(certificationMsgApi);
    }

    HttpOnNextListener<BaseResultEntity> httpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (!baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                loadingLayout.setStatus(LoadingLayout.ERROR);
                return;
            }
            loadingLayout.setStatus(LoadingLayout.SUCCESS);
            if (baseResultEntity.getData()!=null){
                cerBean = (CertificationMsgBean) baseResultEntity.getData();
                Glide.with(context).applyDefaultRequestOptions(RequestOptions.circleCropTransform().error(R.mipmap.deafult_image_header))
                        .load(cerBean.getLogo()).into(imgLogo);
                tvUserName.setText(cerBean.getUserName());  //用户名
                tvFieldLy.setText(cerBean.getMainFields()); //擅长领域
                tvCompanyName.setText(cerBean.getCompany());    //公司
                tvDesc.setText(cerBean.getUserDesc()); //介绍
                tvHonor.setText(cerBean.getAchievementHonor());         //荣誉
                tvIntroduce.setText(cerBean.getIntroduction());       //简介
                tvJobTitle.setText(cerBean.getPosition());       //职称
                tvProjectexperience.setText(cerBean.getProjectExperience()); //项目经历
                tvPublicCation.setText(cerBean.getPublications());           //出版物
                Glide.with(context).applyDefaultRequestOptions(RequestOptions.fitCenterTransform().fallback(null).error(null)).load(cerBean.getHomePageLogo()).into(imgHome); //主页
                Glide.with(context).applyDefaultRequestOptions(RequestOptions.fitCenterTransform().fallback(null).error(null)).load(cerBean.getPersonalResume()).into(imgPersonalResume); //简历
                tvRealName.setText(cerBean.getRealName());  //真实姓名
                FLAG = cerBean.getFlag();
                int userQualificationId = cerBean.getUserQualificationId();
                if (FLAG==3){
                    linearLayout.setVisibility(View.VISIBLE);
                }
                if (FLAG == 1){
                    btnAuthentionhome.setText("认证审核中");
                    btnAuthentionhome.setBackgroundResource(R.drawable.common_main_r20_grey);
                    btnAuthentionhome.setTextColor(getResources().getColor(R.color.black_text));
                    btnAuthentionhome.setEnabled(false);
                }else {
                    btnAuthentionhome.setText("重新认证");
                    btnAuthentionhome.setBackgroundResource(R.mipmap.bg_btlogin);
                    btnAuthentionhome.setTextColor(getResources().getColor(R.color.white));
                    btnAuthentionhome.setEnabled(true);
                }
            }


        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loadingLayout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loadingLayout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

    @Override
    protected void addListener() {
        btnAuthentionhome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //EventBus.getDefault().postSticky(cerBean);
                startActivity(ShenFenRzActivity.getIntent(context,cerBean));
            }
        });
    }


    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, PersonAuthenticationHomeActivity.class);
        return intent;
    }
}
